package de.hsbremen.dirtybox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

public class PictureActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK,
		// MediaStore.Images.Media.INTERNAL_CONTENT_URI);
		// startActivityForResult(getImageFromGalleryIntent, 1);

		Log.d("DEBUG", "HALLLLLLLLLLLLLLLLLLLLLLLLO");
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		startActivityForResult(intent, 1);
	}

	String oldPath = "";

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("DEBUG", "HALLLLLLLLLLLLLLLLLLLLLLLO");
		Toast.makeText(this, oldPath, Toast.LENGTH_SHORT);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case 1:
				oldPath = getPath(data.getData());
				break;
			}
			Log.d("DEBUG", "path: " + oldPath);
			Toast.makeText(this, oldPath, Toast.LENGTH_SHORT);

			String newPath = "mnt/sdcard/MosemannProjekt/test1.jpg";
			String oldPath2 = "mnt/sdcard/WhatsApp/Media/WhatsApp Images/bbb";
			// /mnt/sdcard/dropbox/Sonstiges/IMAG0493-1.jpg
			// /mnt/sdcard/DCIM/teststs/IMAG0346.jpg

			try {
				Log.d("DEBUG", "HUHU 1");
				File sd = Environment.getExternalStorageDirectory();
				if (sd.canWrite()) {
					Log.d("DEBUG", "HUHU 2 mit oldpath= " + oldPath);
					String sourceImagePath = oldPath;
					String destinationImagePath = oldPath2;
					File source = new File(sourceImagePath);
					File destination = new File(destinationImagePath);
					destination.mkdirs();
					File destination2 = new File(destinationImagePath
							+ "/bbb.jpg");
					// destination2.delete();
					Log.d("DEBUG", "HUHU ?1 - source= " + source.getPath());
					Log.d("DEBUG", "HUHU ?2 - dest= " + destination.getPath());
					if (source.exists()) {
						Log.d("DEBUG", "HUHU 3");
						FileChannel src = new FileInputStream(source)
								.getChannel();
						Log.d("DEBUG", "HUHU 4");
						FileChannel dst = new FileOutputStream(destination2)
								.getChannel();
						Log.d("DEBUG", "HUHU 5");
						dst.transferFrom(src, 0, src.size());
						// src.transferTo(0, src.size(), dst);
						destination.exists();
						Log.d("DEBUG", "HUHU 6");
						src.close();
						Log.d("DEBUG", "HUHU 7");
						dst.close();
						Log.d("DEBUG", "HUHU 8");
					}
				}
			} catch (Exception e) {
				Log.d("DEBUG", "ERROR! -> " + e.toString());
			}
		}
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		startManagingCursor(cursor);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}
